package com.eascs.workflow.sys.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.entity.sys.ActOrgRoleUserEntity;
import com.eascs.workflow.sys.dao.SysOrgDao;
import com.eascs.workflow.sys.dao.SysOrgRoleUserDao;
import com.eascs.workflow.sys.dao.SysUserDao;
import com.eascs.workflow.sys.service.UserService;
import com.eascs.workflow.sys.vo.UserVo;

@Service
public class UserServiceImpl implements UserService {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private SysUserDao sysUserDao;
	@Autowired
	private SysOrgDao sysOrgDao;
	@Autowired
	private SysOrgRoleUserDao sysOrgRoleUserDao;

	@Override
	public PageResult<UserVo> getUserVo(String userNo, String orgId, int pageIndex, int pageSize) {
		PageResult<UserVo> pageResult = sysUserDao.getSysUserInSystem(userNo, orgId, pageIndex, pageSize);
		if (pageResult.getData() != null && !pageResult.getData().isEmpty()) {
			List<UserVo> userVoList = pageResult.getData();
			for (UserVo userVo : userVoList) {
				// 用户所属机构
				List<ActOrgEntity> orgList = sysOrgDao.getOrgListByUserNo(userVo.getUserNo());
				// 所属机构字符串
				StringBuffer orgBuffer = new StringBuffer();
				StringBuffer orgRoleBuffer = new StringBuffer();
				for (ActOrgEntity org : orgList) {
					orgBuffer.append(org.getOrgName()).append("<br/>");
					// 查找机构角色
					List<ActOrgRoleUserEntity> orgRoleList = sysOrgRoleUserDao.getUserRoleListWithOrgId(userVo.getUserNo(), org.getOrgId());
					if (orgRoleList != null) {
						orgRoleBuffer.append(org.getOrgName()).append("---");
						for (ActOrgRoleUserEntity oru : orgRoleList) {
							orgRoleBuffer.append(oru.getRoleName()).append("/");
						}
						orgRoleBuffer.append("<br/>");
					}
				}
				userVo.setOrgNames(orgBuffer.toString());
				userVo.setRoleNames(orgRoleBuffer.toString());
			}
		}
		return pageResult;
	}

	@Override
	public void deleteUserResources(String userNo) {
		
	}
}
